﻿<%@ WebHandler Language="C#" Class="AdminHandlerService" %>

using System;
using System.Web;
using System.IO;

using System.Collections;
using System.Collections.Generic;
using System.Reflection;

using XJX.BusinessLayer;

using XJX.Page;

public class AdminHandlerService : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "application/json";

        string response = "";

        String methodName = context.Request["method"];
        Type type = this.GetType();
        MethodInfo method = type.GetMethod(methodName);

        if (method == null)
        {
            response = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Hint, "调用的方法不存在！").ToJson(); return;
        }

        try
        {
            BeforeInvoke(methodName);
            response = method.Invoke(this, null).ToString();
        }
        catch (Exception ex)
        {
            Hashtable result = new Hashtable();
            result["error"] = -1;
            result["message"] = ex.Message;
            result["stackTrace"] = ex.StackTrace;
            String json = XJX.BusinessLayer.JSON.Encode(result);
            context.Response.Clear();
            context.Response.Write(json);
        }
        finally
        {
            AfterInvoke(methodName);
            HttpContext.Current.Response.Write(response);
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

    //权限管理
    protected void BeforeInvoke(String methodName)
    {
        //Hashtable user = GetUser();
        //if (user.role == "admin" && methodName == "remove") throw .      
    }
    //日志管理
    protected void AfterInvoke(String methodName)
    {

    }


    //===============================================================================================================================================
    //===============================================================================================================================================
    //===============================================================================================================================================
    //===============================================================================================================================================
    //===============================================================================================================================================

    //图书到校后，先管理员查询一下，是否有此借阅的书。并显示相关信息。
    public string Ad_Search_Book_From_BID()
    {
        string BID = HttpContext.Current.Request["BID"];//只填编码的后四位或者五位或者全部填入。

        BID = BID.Trim();
        
        int ReturnValue = 0;
        string result = XJX.Page.Admin.Ad_Search_Book_From_BID(BID, out ReturnValue);

        switch (ReturnValue)
        {
            case 1:
                //表示存在记录，不需要处理，直接通过result参数传递JSON数据到页面。
                break;
            case 97:
                result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Hint, "请输入图书编号！").ToJson();
                break;
            case 98:
                result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Hint, "图书编号长度不正确！").ToJson();
                break;
            case 99:
                result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Hint, "根据编号，没有找到符合条件的图书！").ToJson();
                break;
        }

        return result;

    }

    //导出确认表信息到站点目录下ExportToExcel
    public string Get_Admin_Confirm_Yes_List()
    {
        string result = "";

        bool b = XJX.Page.Admin.Get_Admin_Confirm_Yes_List("SaveToBrowse");

        if (b)
        {
            result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Success, "导出确认表数据 成功！").ToJson();
        }
        else
        {
            result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Error, "导出确认表数据时 错误！").ToJson();
        }

        return result;
    }

    //确认图书已到
    public string Confirm_One_Book_Arrived()
    {
        string BorrowID = HttpContext.Current.Request["BorrowID"];//借阅表中的记录号，不是BID编号。是根据上面的方法，得到的一条记录，其中有一个字段名称为：BorrowID, 这个值可以做成隐藏字段。

        string JUID = HttpContext.Current.Request["JUID"];//借阅人编号，隐藏字段
        string CUID = HttpContext.Current.Request["CUID"];//持有人编号，隐藏字段
        string BID = HttpContext.Current.Request["BID"]; //图书编号，隐藏字段
        string Bdate = HttpContext.Current.Request["Bdate"];//借阅开始时间，隐藏字段

        //string BorrowID = "102";
        //string JUID = "802d7ea8-ac30-447a-8579-f6106930da6c";
        //string CUID = "39bfb382-b8d5-4931-a9f6-d5cf4f0b346c";
        //string BID = "BSW000004740";
        //string Bdate = "2018/2/23 10:46:10";
        
        
        FamilyInfo fi = XJX.Page.Family.Get_One_Family_Info_From_UID(JUID);

        string result = "";

        int ReturnValue = XJX.Page.Admin.Confirm_One_Book_Arrived(BorrowID);

        int i = 0; int j = 0;

        switch (ReturnValue)
        {
            case 2:
                //发送短信
                i = XJX.Page.Helper.CallTemplater(2, "toborrowtitle", "toborrowtemplate", JUID, BID, "", DateTime.Parse(Bdate));//发给借阅人的短信。
                j = XJX.Page.Helper.CallTemplater(2, "toholdertitle", "toholdertemplate", CUID, BID, fi.Mobile, DateTime.Parse(Bdate));//发送给图书持有人的短信。

                if (i + j == 2)
                {
                    //前台显示成功信息。
                    result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Success, "借出 确认图书成功！").ToJson();
                }
                else
                {
                    result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Success, "借出成功，但短信发送失败！").ToJson();
                }
                break;
            case 3:
                //发送短信
                i = XJX.Page.Helper.CallTemplater(4, "toborrowtitle", "toborrowtemplate", JUID, BID, "", DateTime.Parse(Bdate));//发给借阅人的短信。
                j = XJX.Page.Helper.CallTemplater(4, "toholdertitle", "toholdertemplate", CUID, BID, fi.Mobile, DateTime.Parse(Bdate));//发送给图书所有人的短信。

                if (i + j == 2)
                {
                    //前台显示成功信息。
                    result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Success, "还入 确认图书成功！").ToJson();
                }
                else
                {
                    result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Success, "还入成功，但短信发送失败！").ToJson();
                }

                break;
            case 50:
                result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Hint, "图书还在确认表中，不能再次确认！").ToJson();
                break;
            case 98:
                result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Error, "更新图书状态时错误！").ToJson();
                break;
            case 99:
                result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Error, "查询处于非正常状态！").ToJson();
                break;
        }

        return result;
    }

    //清除当前确认表
    public string Ad_Clear_Confirm_Table_Info()
    {
        string result = "";
        int ReturnValue=-1;

        //将确认表在服务器端备份一次。
        bool b=XJX.Page.Admin.Get_Admin_Confirm_Yes_List("SaveToFile");
        

        if (b)
        {
            ReturnValue = XJX.Page.Admin.Ad_Clear_Confirm_Table_Info();
        }

        if (ReturnValue > 0)
        {
            result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Success, "清除图书借阅信息  成功！").ToJson();
        }
        else
        {
            result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Hint, "清除图书借阅信息  失败！").ToJson();
        }

        return result;
    }
    
    //导入学生数据到数据库中
    public string Import_Student_Info_From_Excel()
    {
        string result = "";

        try
        {
            int Value = 0;
            string Msg = "";
            HttpPostedFile file = HttpContext.Current.Request.Files["UploadExcelFileInfo"];//
            
            Helper.uploadExcel(file,out Value, out Msg);

            result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Hint,Msg).ToJson();
        }
        catch (System.Exception ex)
        {
            result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Error, "导入家庭信息时发生严重错误，请联系管理员解决！" + ex.Message).ToJson();
        }

        return result;
    }


    //管理员验证登录
    public string Ad_Verification_User_Log()
    {
        string mobile = HttpContext.Current.Request["phoneNumber"];

        string pwd = HttpContext.Current.Request["inputPassword"];

        //下面开始调用验证
        string ReturnFieldValue="";
        int ReturnValue = XJX.Page.Admin.Ad_Verification_User_Log(mobile,Helper.MD5(pwd),out ReturnFieldValue);

        string result = "";

        //返回值给前端处理
        switch (ReturnValue)
        {
            case 0:
                result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Error, "手机号或密码错误!").ToJson();
                break;
            case 1:
                    AdminInfo ai = new AdminInfo();
                    ai = XJX.Page.Admin.Ad_2_Get_Admin_Info_0(ReturnFieldValue);
                    HttpContext.Current.Session["AdminInfo"] = ai;//保持基础信息

                //返回前端处理
                result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Success, "管理员登录验证成功!").ToJson();
                break;
            default:
                result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.OtherError, "未知原因导致登录失败!").ToJson();
                break;
        }
        return result;
    }

    //退出登录
    public string ExitLogStatus()
    {
        HttpContext.Current.Session["AdminInfo"] = null;

        return new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Hint, "您已经退出登录，网页将转到登录页!").ToJson();
    }
    
    //获取管理员基础信息
    public string Ad_2_Get_Admin_Info()
    {
        string result="";

        AdminInfo ai = new AdminInfo();
        ai = (AdminInfo)HttpContext.Current.Session["AdminInfo"];

        result = XJX.Page.Admin.Ad_2_Get_Admin_Info_1(ai.SchoolID);
        
        return result; 
    }    
    
    
}